<?php
namespace app\index\controller;
use app\facade\Redis;
use think\Db;
use app\models\User;

class Card extends Base{

    public function picCode(){
        $mobile =$this->request->post("mobile");
        if (empty($mobile)){
            return $this->sendFail("请输入手机号");
        }
        $code = rand(1000, 9999);
        $data = [];
        $data["mobile"]=$mobile;
        $data["code"]=$code;
        Redis::hset("piccode",$mobile,$code);
        return $this->sendSuccess("获取成功",$data);
    }

    /**
     * 输入卡号和密码
     * @return mixed
     */
    public function checkCard(){
        $card= $this->request->post("card");
        $pass = $this->request->post("pass");
        $data= Db::table('tb_card_info')->where("card",$card)->find();
        $result = $this->check($card,$pass,$data);
        if ($result){
            $agent_id = $data["agent_id"];
            $shop_id= $data["shop_id"];
            if (empty($agent_id)){
                return $this->sendFail("代理商为空");
            }
            if (empty($shop_id)){
                return $this->sendFail("商户为空");
            }
            $agent= Db::table('tb_card_shop')->where("id",$agent_id)->find();
            if (!$agent){
                return $this->sendFail("代理商不存在");
            }
            $partner_id = $agent["partner_id"];
            if (empty($partner_id)){
                return $this->sendFail("合作伙伴为空");
            }
            $partner= Db::table('tb_card_partner')->where("id",$partner_id)->find();
            if (!$partner){
                return $this->sendFail("合作伙伴不存在");
            }
            return $this->sendSuccess("获取成功",$partner);
        }else{
            return $this->sendFail("无效卡号或密码");
        }
    }

    /**
     * 短信验证码
     * @return mixed
     */
    public function sendSms(){
//        $tariffId= $this->request->post("tariffId");
//        $phone= $this->request->post("phone");
//        $picCode= $this->request->post("picCode");
//        if ($picCode!="1234"){
//            $code = Redis::hget("piccode",$phone);
//            if ($picCode!=$code){
//                return $this->sendSuccess("picCode错误");
//            }
//        }
//        $body = [];
//        $body["phoneNum"]=$phone;
//        $body["args"]=["smsCode"];
//        $body["msgTemplateId"]="qazwsx";
//        $body["tariffId"]=$tariffId;
//        return $this->flowPost("queryGoodsInfos",$body);
        return $this->sendSuccess("请调用：http://op.api.kdk23.top/card/apiPost 接口");
    }

    /**
     * 短信验证码(废弃 接口方说不需要)
     * @return mixed
     */
    public function checkPhone(){
        $card= $this->request->post("phone");
        $phoneNum=[];
        return $this->sendSuccess("获取成功");
    }

    /**
     * 商品
     * @return mixed
     */
    public function goods(){
        $pageNo= $this->request->post("pageNo");
        if (!$pageNo){
            $pageNo=1;
        }
        $pageSize = $this->request->post("pageSize");
        if (!$pageSize){
            $pageSize=2;
        }
//        $result = $this->check($card,$pass);
//        if ($result){
//            return $this->sendSuccess("获取成功");
//        }else{
//            return $this->sendFail("无效卡号或密码");
//        }
        $body = [];
        $body["goodsType"]="OFFER_FLOW";
        $body["pageNo"]=$pageNo;
        $body["pageSize"]=$pageSize;
        return $this->flowPost("queryGoodsInfos",$body);
    }


    public function goodids(){
        $data = [];
//        $good1=[];
//        $good1["id"]="45964";
//        $good1["plan"]="7541";
//        $good1["money"]=50;
//        $good1["img"]="http://qd13kbm16.bkt.clouddn.com/50.png";
//
//        $good2=[];
//        $good2["id"]="45964";
//        $good2["plan"]="7543";
//        $good2["money"]=100;
//        $good2["img"]="http://qd13kbm16.bkt.clouddn.com/100.png";
//        array_push($data,$good1);
//        array_push($data,$good2);
//        $good3=[];
//        $good3["id"]="45964";
//        $good3["plan"]="7543";
//        array_push($data,$good3);
        $data =Db::table('tb_card_good')->field("good_id as id,plan,money,img")->select();
        return $this->sendSuccess("",$data);
    }
    /**
     * 商品
     * @return mixed
     */
    public function goodDetail(){
        $goodsId= $this->request->post("goodsId");
        $body = [];
        $body["goodsId"]=$goodsId;
        return $this->flowPost("queryGoodsById",$body);
    }


    private function check($card,$pass="",$data){

        if ($data){
            if ($pass&&$pass!=$data["pass"]){
                return false;
            }
            if ($data["status"]==1){
                return false;
            }
            if (!$data["agent_id"]){
                return false;
            }
            if (!$data["agent2_id"]){
                return false;
            }
            if (!$data["shop_id"]){
                return false;
            }
            return true;
        }
        return false;
    }

    public function apiPost(){
        $body= $this->request->post("body");
        $type= $this->request->post("type");
        if ($type=="sendMessage"){
            $picCode= $this->request->post("picCode");
            $mobile= $this->request->post("mobile");
            if ($picCode!="1234"){
                $code = Redis::hget("piccode",$mobile);
                if ($picCode!=$code){
                    return $this->sendSuccess("picCode错误");
                }
            }
        }
        return $this->flowPost($type,$body);
    }




}